package com.sea.finance.controller;

import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.finance.entity.Fund;
import com.sea.finance.entity.FundType;
import com.sea.finance.service.FundService;
import com.sea.finance.service.FundTypeService;
import com.sea.product.entity.Category;
import com.sea.supply.entity.Bill;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/finance/fundType")
public class FundTypeController {

    @Autowired
    private FundTypeService fundTypeService;

    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        return "/sea/finance/fundType/main";
    }

    @RequestMapping("/report")
    public String report(Model model) {
        return "/sea/finance/fundType/report";
    }

    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     FundType fundType) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<FundType> list = fundTypeService.findByCnd(fundType);
        PageInfo<FundType> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    @ResponseBody
    @RequestMapping("/findByShop")
    public AjaxTableReturn findByShop(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<FundType> list = fundTypeService.findByAllShop();
        PageInfo<FundType> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }
    @ResponseBody
    @RequestMapping("/findByMoneyType")
    public List<FundType> findByMoneyType(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                         @RequestParam(value = "offset", defaultValue = "0") int start,
                                         String fundShop) {
        return fundTypeService.findByMoneyType(fundShop);
    }
    @ResponseBody
    @RequestMapping("/findByShopMoneyType")
    public List<FundType> findByShopMoneyType(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                          @RequestParam(value = "offset", defaultValue = "0") int start,
                                          FundType fundType) {
        return fundTypeService.findByShopMoneyType(fundType);
    }
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @ResponseBody
    public void export(HttpServletResponse response, FundType fundType) {
        List<FundType> list = fundTypeService.findByCnd(fundType);
        String[] title = {"公司","币种","余额"};
        String fileName = "资金管理" + DateUtils.dateToString(new Date(), "yyyyMMdd") + ".xls";
        String sheetName = "资金管理";
        String[][] content = new String[list.size()][3];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            FundType obj = list.get(i);
            content[i][0] = obj.getFundShopName();
            content[i][1] = obj.getFundMoneyType();
            content[i][2] = obj.getFundTotal()+"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
